package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    //根据分类id和status=1查询菜品信息
    @GetMapping("/dish/list")
    public ResultInfo findList(String name ,Long categoryId, @RequestParam(defaultValue = "1") Integer status) {
       //先从Redis中查询
        String key = "dish_" + categoryId;//保存到redis中的键
        List<Dish> dishList = (List<Dish>) redisTemplate.opsForValue().get(key);

        if (CollectionUtils.isNotEmpty(dishList)) {
            //存在
            return ResultInfo.success(dishList);
        } else {
            //不存在
            dishList = dishService.findList(categoryId, status, name);
            redisTemplate.opsForValue().set(key,dishList);
            return ResultInfo.success(dishList);

        }

    }
}
